﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Data;
using BitmapTagger;

namespace ImageTagEditor.ValueConverters
{
	/// <summary>
	/// Converts objects of type ushort? to strings and back
	/// </summary>
	public class NullableUShortValueConverter : IValueConverter
	{

		/// <summary>
		/// Convert an object of type ushort? to a string
		/// </summary>
		/// <param name="value">The ushort? to convert</param>
		/// <param name="targetType">Not Used</param>
		/// <param name="parameter">Not Used</param>
		/// <param name="culture">Not Used</param>
		/// <returns>The string converted from the ushort?</returns>
		public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value == null)
				return "";
			if (value is ushort?)
			{
				ushort? ushortValue = value as ushort?;
				return ushortValue.ToString();
			}
			throw new Exception("NullableUShortValueConverter can only convert objects of type ushort?");
		}

		/// <summary>
		/// Convert back from a string to a ushort?
		/// </summary>
		/// <param name="value">The string to convert back from</param>
		/// <param name="targetType">Not used</param>
		/// <param name="parameter">Not used</param>
		/// <param name="culture">Not used</param>
		/// <returns>The ushort? that corresponds to the passed string</returns>
		public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
		{
			if (value is string)
			{
				string stringValue = value as string;
				if (string.IsNullOrEmpty(stringValue))
					return null;
				else
					return ushort.Parse(stringValue);
			}
			throw new Exception("NullableUShortValueConverter can only convert back from objects of type string");
		}

	}
}
